package edu.cuit.campus.admin.controller;

import com.alibaba.fastjson2.JSONObject;
import edu.cuit.campus.admin.service.JobCategoryService;
import edu.cuit.campus.admin.service.bean.JobCategoryServiceBean;
import edu.cuit.campus.commons.StringUtils;
import edu.cuit.campus.commons.annotations.Controller;
import edu.cuit.campus.commons.annotations.RequestMapping;
import edu.cuit.campus.commons.annotations.ResponseBody;
import edu.cuit.campus.commons.response.Result;
import edu.cuit.campus.entity.po.JobCategory;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/9 14:14
 */
@Controller
@RequestMapping("/job-categories/")
public class JobCategoryController {
    private JobCategoryService service = new JobCategoryServiceBean();

    @RequestMapping("/list")
    @ResponseBody
    public Result list(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //1.接收用户请求
        var pageIndex = request.getParameter("pageIndex") == null ? 1 : Integer.parseInt(request.getParameter("pageIndex"));
        var pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request.getParameter("pageSize"));
        var categoryName = request.getParameter("name");

        return Result.ok(service.page(pageIndex, pageSize, categoryName));
    }

    /**
     * 保存职位类别信息
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //1.接收用户请求
        BufferedReader reader = request.getReader();
        String json = "";
        String line = null;
        while((line = reader.readLine()) != null)
            json += line;
        if(!StringUtils.isBlank(json)){
            JobCategory category = JSONObject.parseObject(json, JobCategory.class);
            service.save(category);
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根据id删除职位类别信息
     */
    @RequestMapping("/remove")
    @ResponseBody
    public Result removeById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        var id = Integer.valueOf(request.getParameter("id"));
        return service.removeById(id) ? Result.ok() : Result.fail();
    }

    /**
     * 根据id删除职位类别信息
     */
    @RequestMapping("/parent/list")
    @ResponseBody
    public Result getParentCategories(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        var result = service.getParentCategories();
        return Result.ok(result);
    }

    /**
     * 根据id删除职位类别信息
     */
    @RequestMapping("/find")
    @ResponseBody
    public Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        var id = Integer.valueOf(request.getParameter("id"));
        return Result.ok(service.findById(id));
    }

    /**
     * 根据id删除职位类别信息
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //1.接收用户请求
        BufferedReader reader = request.getReader();
        String json = "";
        String line = null;
        while((line = reader.readLine()) != null)
            json += line;
        if(!StringUtils.isBlank(json)){
            JobCategory category = JSONObject.parseObject(json, JobCategory.class);
            service.edit(category);
            return Result.ok();
        }
        return Result.fail();
    }
}
